package catchData.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

import util.CatchDataUtil;

public class CatchGet1 implements Runnable {
	
	private Logger LOG = Logger.getLogger("CatchGet1");
	private static CatchDataUtil catchData = new CatchDataUtil();
//	private static String htmlForder = "E:/downloadData";
//	private static String webURL = "http://www.cdjq.net/peisong/ps.asp?page=";
	
	private String webURL;
	private String folderPath;
	private int startPage;
	private int endPage;
	private String encode;
	private String otherParams;
	
	public CatchGet1(String webURL, String folderPath, int startPage, int endPage, String encode, String otherParams) {
		this.webURL = webURL;
		this.folderPath = folderPath;
		this.startPage = startPage;
		this.endPage = endPage;
		this.encode = encode;
		this.otherParams = otherParams;
	}
	
	 /**
	  * 将InputStream信息转换为String信息。
	  *
	  * @param is
	  * @return
	  * @throws UnsupportedEncodingException
	  * @since 1.0
	  */
	 public String convertStreamToString(InputStream is)
	   throws UnsupportedEncodingException {

	  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"ISO-8859-1"));
	  StringBuilder sb = new StringBuilder();

	  String line = null;
	  try {
	   while ((line = reader.readLine()) != null) {
	    sb.append(line + "\n");
	   }
	  } catch (IOException e) {
	   e.printStackTrace();
	  } finally {
	   try {
	    is.close();
	   } catch (IOException e) {
	    e.printStackTrace();
	   }
	  }
	  
	  LOG.info(new String(sb.toString().getBytes("ISO-8859-1"),"GB2312"));
	  return sb.toString();
	 }
	
	public void run() {
		StringBuilder str = new StringBuilder();
		GetMethod method = null;
		HttpClient client = catchData.initHttpClient();
		try {
			for (int i = startPage; i <= endPage; i++) {
				LOG.info(webURL+i+otherParams);
				method = new GetMethod(webURL+i+otherParams);
				catchData.setHeaders(method, webURL+i+otherParams, catchData.getWebSite(webURL));
				client.executeMethod(method);
				str.append(method.getResponseBodyAsString());
//				LOG.info(str);
				catchData.saveDataFile(folderPath, str.toString(), i, encode);
				str.delete(0, str.length());
				method.releaseConnection();
			}
		} catch (Exception e) {
			LOG.info(e);
		}
	}

	public String getWebURL() {
		return webURL;
	}

	public void setWebURL(String webURL) {
		this.webURL = webURL;
	}

	public String getFolderPath() {
		return folderPath;
	}

	public void setFolderPath(String folderPath) {
		this.folderPath = folderPath;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

	public String getEncode() {
		return encode;
	}

	public void setEncode(String encode) {
		this.encode = encode;
	}

	public String getOtherParams() {
		return otherParams;
	}

	public void setOtherParams(String otherParams) {
		this.otherParams = otherParams;
	}
}